package com.xoa.service.inv.impl;

import com.xoa.dao.crm.CustomerMapper;
import com.xoa.dao.inv.CusPaymentMapper;
import com.xoa.dao.inv.CusReceiptMapper;
import com.xoa.dao.inv.CusRepaymentMapper;
import com.xoa.dao.inv.InvWarehouseMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.inv.CusPayment;
import com.xoa.model.inv.CusReceipt;
import com.xoa.model.inv.CusReceiptExample;
import com.xoa.model.inv.InvWarehouse;
import com.xoa.model.users.Users;
import com.xoa.service.crm.CustomerService;
import com.xoa.service.inv.CusReceiptService;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CusReceiptServiceImpl implements CusReceiptService {
    @Autowired
    CusReceiptMapper cusReceiptMapper;
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    CusPaymentMapper cusPaymentMapper;
    @Autowired
    InvWarehouseMapper invWarehouseMapper;
    @Override
    public ToJson<CusReceipt> insertCusReceipt(CusReceipt cusReceipt) {
        ToJson<CusReceipt> toJson=new ToJson<>();
        int insert = cusReceiptMapper.insert(cusReceipt);
        if (insert >0){
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }
        return toJson;
    }

    @Override
    public ToJson<CusReceipt> updateCusReceipt(CusReceipt cusReceipt) {
        ToJson<CusReceipt> toJson=new ToJson<>();
        int insert = cusReceiptMapper.updateByPrimaryKey(cusReceipt);
        if (insert >0){
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }
        return toJson;
    }

    @Override
    public ToJson<CusReceipt> selectCusRecepit(CusReceipt cusReceipt, Integer page, Integer limit,String month,String year) {
        ToJson<CusReceipt> toJson=new ToJson<>();
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> map2=new HashMap<>();
        PageParams pageParams=new PageParams();
        pageParams.setPageSize(limit);
        pageParams.setPage(page);
        pageParams.setUseFlag(true);
        map.put("pageParams",pageParams);
        map.put("cusReceipt",cusReceipt);
        map2.put("cusReceipt",cusReceipt);


        if (!StringUtils.checkNull(year)){
            map.put("year",year);
            map2.put("year",year);

        }
        if (!StringUtils.checkNull(month)){
            map.put("month",month.split("-")[1]);
            map2.put("month",month.split("-")[1]);
            map.put("year",month.split("-")[0]);
            map2.put("year",month.split("-")[0]);
        }

        List<CusReceipt> cusReceiptExamples = cusReceiptMapper.selectAllCusReceipt(map);
        for (CusReceipt receiptExample:cusReceiptExamples){
            if (!StringUtils.checkNull(receiptExample.getUserId())){
                Users byUserId = usersMapper.getByUserId(receiptExample.getUserId());
                if(byUserId!=null){
                    receiptExample.setUserName(byUserId.getUserName());
                }
            }
            if (!StringUtils.checkNull(receiptExample.getWareHouseId())){
                receiptExample.setWareHouseName(invWarehouseMapper.selectByPrimaryKey(Integer.parseInt(receiptExample.getWareHouseId())).getWarehouseName());
            }
        }
        toJson.setMsg("ok");
        toJson.setFlag(0);
        toJson.setObject(cusReceiptExamples);
        toJson.setTotleNum(cusReceiptMapper.selectAllCusReceiptCount(map2));
        return toJson;
    }

    @Override
    public ToJson<CusReceipt> deleteCusRecepit(String receiptId) {
        ToJson<CusReceipt> toJson=new ToJson<>();
        int insert = cusReceiptMapper.deleteByPrimaryKey(Integer.parseInt(receiptId));
        if (insert >0){
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }
        return toJson;
    }

    @Override
    public ToJson<CusReceipt> selectOneCusRecepit(CusReceipt cusReceipt) {
        ToJson<CusReceipt> toJson=new ToJson<>();
        CusReceipt cusReceipt1 = cusReceiptMapper.selectByPrimaryKey(cusReceipt.getReceiptId());
        if (!StringUtils.checkNull(cusReceipt1.getUserId())){
            cusReceipt1.setUserName(usersMapper.getByUserId(cusReceipt1.getUserId()).getUserName());
        }
        if (!StringUtils.checkNull(cusReceipt1.getWareHouseId())){
            cusReceipt1.setWareHouseName(invWarehouseMapper.selectByPrimaryKey(Integer.parseInt(cusReceipt1.getWareHouseId())).getWarehouseName());
        }

        toJson.setMsg("ok");
        toJson.setFlag(0);
        toJson.setObject(cusReceipt1);
        return toJson;
    }

    public ToJson<CusPayment> insertCusPayment(CusPayment cusPayment) {
        ToJson<CusPayment> toJson=new ToJson<>();
        int insert = cusPaymentMapper.insert(cusPayment);
        if (insert >0){
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }
        return toJson;
    }

    @Override
    public ToJson<CusPayment> updateCusPayment(CusPayment cusPayment) {
        ToJson<CusPayment> toJson=new ToJson<>();
        int insert = cusPaymentMapper.updateByPrimaryKey(cusPayment);
        if (insert >0){
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }
        return toJson;
    }

    @Override
    public ToJson<CusPayment> selectCusPayment(CusPayment cusPayment, Integer page, Integer limit,String month,String year) {
        ToJson<CusPayment> toJson=new ToJson<>();
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> map2=new HashMap<>();
        PageParams pageParams=new PageParams();
        pageParams.setPageSize(limit);
        pageParams.setPage(page);
        pageParams.setUseFlag(true);
        map.put("pageParams",pageParams);
        map.put("cusReceipt",cusPayment);
        map2.put("cusReceipt",cusPayment);

        if (!StringUtils.checkNull(year)){
            map.put("year",year);
            map2.put("year",year);

        }
        if (!StringUtils.checkNull(month)){
            map.put("month",month.split("-")[1]);
            map2.put("month",month.split("-")[1]);
            map.put("year",month.split("-")[0]);
            map2.put("year",month.split("-")[0]);

        }

        List<CusPayment> cusReceiptExamples = cusPaymentMapper.selectAllCusPayment(map);
        for (CusPayment receiptExample:cusReceiptExamples){
            if (!StringUtils.checkNull(receiptExample.getUserId())){
                receiptExample.setUserName(usersMapper.getByUserId(receiptExample.getUserId()).getUserName());
            }
            if (!StringUtils.checkNull(receiptExample.getWareHouseId())){
                receiptExample.setWareHouseName(invWarehouseMapper.selectByPrimaryKey(Integer.parseInt(receiptExample.getWareHouseId())).getWarehouseName());
            }
        }
        toJson.setMsg("ok");
        toJson.setFlag(0);
        toJson.setObject(cusReceiptExamples);
        toJson.setTotleNum(cusPaymentMapper.selectAllCusPaymentCount(map2));
        return toJson;
    }

    @Override
    public ToJson<CusPayment> deleteCusPayment(String paymentId) {
        ToJson<CusPayment> toJson=new ToJson<>();
        int insert = cusPaymentMapper.deleteByPrimaryKey(Integer.parseInt(paymentId));
        if (insert >0){
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }
        return toJson;
    }

    @Override
    public ToJson<CusPayment> selectOneCusPayment(CusPayment cusPayment) {
        ToJson<CusPayment> toJson=new ToJson<>();
        CusPayment cusPayment1 = cusPaymentMapper.selectByPrimaryKey(cusPayment.getPaymentId());
        if (!StringUtils.checkNull(cusPayment1.getUserId())){
            cusPayment1.setUserName(usersMapper.getByUserId(cusPayment1.getUserId()).getUserName());
        }
        if (!StringUtils.checkNull(cusPayment1.getWareHouseId())){
            cusPayment1.setWareHouseName(invWarehouseMapper.selectByPrimaryKey(Integer.parseInt(cusPayment1.getWareHouseId())).getWarehouseName());
        }

        toJson.setMsg("ok");
        toJson.setFlag(0);
        toJson.setObject(cusPayment1);
        return toJson;
    }

}
